<?php
/**
 * Created by PhpStorm.
 * User: Chris
 * Date: 14-3-23
 * Time: 下午5:55
 */
class Controller_Admin_Base extends Controller_Template
{
    public $template = "template_admin";

    protected function response_json($data){
        $body = \Format::forge($data)->to_json();

        $response = new Response($body, 200);
        $response->set_header('Content-Type', 'application/json');
        return $response;
    }

    public function checkPermission($right, $redirectIfNotGrant=true){
        $auth = Auth::instance('Simpleauth');
        if ($auth->has_access($right)){
            return true;
        }
        if ($redirectIfNotGrant){
            throw new PermissionRequireException();
        }
        return false;
    }

    public function before(){
        $auth = Auth::instance('Simpleauth');

//        if ( ! Auth::instance()->check() or ! Auth::instance()->guest_login() )
        if ( ! $auth->check() or $auth->guest_login() )
        {
            Response::redirect("admin/login");
        }

        parent::before();
    }

    public static function forgePagination($uri, $pagesize = 10){
        return Pagination::forge('pagination', array(
            'pagination_url' => Uri::base() . $uri,
            'per_page' => $pagesize,
            'show_first'=>true,
            'show_last'=>true,
            //            'uri_segment' => 3,
            // or if you prefer pagination by query string
            'uri_segment' => 'offset',
        ));
    }






}